package cpa.prowon.gui.actions;

import java.util.Iterator;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MessageBox;

import pho.prowon.category.VisibleIndexCategory;
import cpa.prowon.Application;
import cpa.prowon.gui.MainWindow;

/**
 * The remove action is used to remove an index category
 * from the index
 * @author Chris
 */
public class RemoveCategoryAction
	extends PatchedAction
	implements ISelectionChangedListener {

	/**
	 * Constructor
	 * @param MainWindow The window where the action is used
	 * @author
	 */
	public RemoveCategoryAction(MainWindow w) {
		super(w);
		setEnabled(false);
	}

	/**
	 * Return the title key
	 * @return The title key
	 * @author Chris
	 */
	protected String getTitleKey() {
		return "menu_item_edit_removecategory";
	}

	/**
	 * Return the tooltip key
	 * @return The tootip key
	 * @author Chris
	 */
	protected String getTooltipKey() {
		return "menu_item_edit_removecategory_tooltip";
	}

	/**
	 * Return the image key
	 * @return The image key
	 * @author Chris
	 */
	protected String getImageKey() {
		return "";
	}

	/**
	 * Run the action
	 * @author Chris
	 */
	public void run() {
		
		// Ask the user whether he is really sure to remove the selected index category
		MessageBox msgBox = new MessageBox(window.getShell(), SWT.OK | SWT.CANCEL);
		msgBox.setText(Application.getInstance().getResourceBundle().getString("title_removecategory"));
		msgBox.setMessage(Application.getInstance().getResourceBundle().getString("msg_removecategory"));
		int answer = msgBox.open();
		
		// If the user has chosen no, then return
		if (answer != SWT.OK) {
			return;
		}
		
		// Get the selected image(s)
		IStructuredSelection selection = window.getCategorizationViewerSelection();
		
		// Remove the index category
		Iterator ii = selection.iterator();
		while (ii.hasNext()) {
			VisibleIndexCategory cat = (VisibleIndexCategory) ii.next();
			cat.removeAll();
		}

		// Refresh the picture list viewer
		window.refreshListPart();
		
		// Refresh the category viewer, in case the category is not needed anymore, it will get removed
		window.refreshCategoryPart();
				
		// Save the index
		window.getCategoryPart().getRootIndexCategory().writeIndex();
			
	}
	
	/**
	 * Listen to the selection changed event in order to enable or disable the action
	 * @param SelectionChangedEvent The event that occurs when the selection changed
	 * @author Chris
	 * 
	 */
	public void selectionChanged(SelectionChangedEvent event) {

		// Get the selection and set the action enabled or disabled depending on this
		IStructuredSelection selection = window.getCategorizationViewerSelection();
		if (selection.size() != 1) {
			setEnabled(false);
			return;
		}
		setEnabled(true);

	}
	
}
